#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if (root == nullptr)  return ret;
        queue<TreeNode*> q;
        q.push(root);
        int flag = 1;
        while (q.size())
        {
            vector<int> tmp;
            int k = q.size();
            while (k--)
            {
                TreeNode* t = q.front(); q.pop();
                tmp.push_back(t->val);
                if (t->left)  q.push(t->left);
                if (t->right)  q.push(t->right);
            }
            if (flag % 2 == 0)
            {
                reverse(tmp.begin(), tmp.end());

            }
            ret.push_back(tmp);
            flag++;
        }
        return ret;
    }
};